// RSA加密https://www.cnblogs.com/crazy-girl/p/11589423.html
import JSEncrypt from 'jsencrypt';
import store from '@/store'
import Vue from 'vue'
//获取公钥
function getRsaCode() {
	return new Promise((result, reject) => {
		Vue.prototype.GLOBAL.doGet(store.getters.api.GLOBAL_key_randomPubKey, {}, function(res) {
			if (res.code == 200) {
				result(res.data)
			}else{
				reject(res.msg)
			}
		});
	})
}
//传入要加密字符串
async function rsaEncode(str) {
	// let pubKey = await getRsaCode() //rsa 公钥
	let pubKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgUJisBVFRUHIaHG7swKN0elX/HuFNiYbs5dL2ziQjbgF8LoQ3mCgJ6Q78c9ucIi2avQ7cBvZVfLSEEIXI1elOt7q1hHa4RmqjCbGjocnDlDDqGjb9UI3Ur/Ok0i0hzTbc2lihFgfWppJRp5CnjXlRft1b0nnRqpxoD8cxypKf9NYjVHMWf/jiu3ujhVANQVD4CyijkB3JJYmkSOBGFz3TTEsHkU8C9NUw+4L76X6Y9RtFGUIwBO6pKMR10QFe4ck/sRvMktU6/ywCDV/hZbWv26Umn6JEP4S/DI+j0EUfeC458RHvZTPGHWiq/UWx3sw9pwSVFfKOsYKsChbZ6WhNwIDAQAB' //rsa 公钥
	let jse = new JSEncrypt();
	jse.setPublicKey(pubKey); // 设置 加密公钥
	let data = "";
	/**
	 * 如果加密字符串长度超过117个前端每117字符进行加密，
	 * 后端需要每172个进行截取分段解密
	 * **/
	for (var i = 0; i < Math.ceil(str.toString().length / 117); i++) {
		let origin = str.toString().slice(i*117,i*117 + 117);
		data += jse.encrypt(origin);// 进行加密
	}
	// let data = jse.encrypt(str.toString()); // 进行加密
	return data
}
//rsa解密
async function rsaDecode(str) {
	let jse = new JSEncrypt();
	//使用前端私钥解密（私钥由服务端生成）
	let privKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCBQmKwFUVFQchocbuzAo3R6Vf8e4U2Jhuzl0vbOJ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ce86GV3vK9dE6yndGd0T+NtWatJ3qn+joWO5Zz+wAdA/jmu1kqOyF5UWZ0W8JyppXdSASR8vfhKFS3frQKBgHA6c8XqKLy//zJC5Pip7JHbSowN/v4FpSEIuKAhErf4IiCdVQellk4Ki8M8BFTOek5gq+6nEq7H2VkbdDnDublth1JAAj7C1mlgIn587aigJ0EakhN0WI9rmq1OFjhcrDxKoLiKYAscwqy5rqx2cx0/MPev0TDXJEoXt8fpo86xAoGAVIVqC6Tc1S1uuLg7XSbyoATzKRC0ibdEH+Cr4KF6Fu7qSIQKfhH9/Px5GA1dsZ8YcLGuqkIl+om4NazBj+RgytyAYXlZs/aEQkWZbz7qQpxjG9dCf0s/81jcSjnwYBxsWsbl85rmW6S7qjnAf3SNIN77zWX46qTrjyy3SCAo6gA='
	jse.setPrivateKey(privKey);
	let data = jse.decrypt(str); //解密
	console.log(data);
	return data
}


export default {
	rsaDecode,
	rsaEncode
}
